package com.zzh.takeoutapp.activity;

import android.content.Intent;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zzh.takeoutapp.R;
import com.zzh.takeoutapp.adapter.CollectOrderAdapter;
import com.zzh.takeoutapp.adapter.FoodAdapter;
import com.zzh.takeoutapp.adapter.order.more.CheckListener;
import com.zzh.takeoutapp.base.BaseActivity;
import com.zzh.takeoutapp.entity.CollectOrderEntity;
import com.zzh.takeoutapp.event.NetMsgEvent;
import com.zzh.takeoutapp.service.EventbusHelper;
import com.zzh.takeoutapp.service.ServiceUrl;
import com.zzh.takeoutapp.utils.BaseUtils;
import com.zzh.takeoutapp.utils.GsonResolve;
import com.zzh.takeoutapp.utils.LoadingUtil;
import com.zzh.takeoutapp.utils.ToolBarHelper;
import com.zzh.takeoutapp.view.goods.ViewUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/7/30.
 * 我的收藏订单
 */

public class CollectOrderActivity extends BaseActivity implements CheckListener, OnRefreshListener, OnLoadmoreListener {
    @Bind(R.id.refreshLayout)
    RefreshLayout mRefreshLayout;
    @Bind(R.id.toolbar)
    Toolbar tbToolbar;
    @Bind(R.id.recyclerView)
    RecyclerView mRv;
    @Bind(R.id.empty_text)
    NestedScrollView mEmptyLayout;//设置没有数据网络的情况
    private CollectOrderAdapter mAdapter;
    private LinearLayoutManager mManager;
    private boolean move;
    private int index;
    private TextView tvStickyHeaderView;
    private View stickView;
    private List<CollectOrderEntity> collectOrderEntities;
    private LoadingUtil loadingUtil;

    @Override
    public void setContent() {
        setContentView(R.layout.activity_collect_order);
        ButterKnife.bind(this);
        loadingUtil = new LoadingUtil(this);
    }

    @Override
    public void initComponent() {
        if (getIntent().getStringExtra("mine_collect").equals("mine_collect")) {
            ToolBarHelper.setTitleAndBackAndText(this, tbToolbar, "我的收藏");
            loadingUtil.startShowLoading();
            ServiceUrl.LoadcollectSerVice(this, "MINE_COLLECT");
            collectOrderEntities = new ArrayList<>();
        } else {
            ToolBarHelper.setTitleAndBackAndText(this, tbToolbar, "我的足迹");
            ServiceUrl.LoadcollectSerVice(this, "MINE_COLLECT");
        }
        mManager = new LinearLayoutManager(this);
        mRv.setLayoutManager(mManager);
    }

    @Override
    public void setListener() {
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.setOnLoadmoreListener(this);
        mRefreshLayout.setDisableContentWhenRefresh(true);
        mRefreshLayout.setDisableContentWhenLoading(true);
        mRefreshLayout.setEnableLoadmore(false);
        stickView = findViewById(R.id.stick_header);
        tvStickyHeaderView = findViewById(R.id.tv_header);
    }

    @Override
    public void check(int position, boolean isScroll) {

    }


    @Override
    public void onAfterInit() {
        mRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                View stickyInfoView = recyclerView.findChildViewUnder(stickView.getMeasuredWidth() / 2, 5);
                if (stickyInfoView != null && stickyInfoView.getContentDescription() != null) {
                    tvStickyHeaderView.setText(String.valueOf(stickyInfoView.getContentDescription()));
                }
                View transInfoView = recyclerView.findChildViewUnder(stickView.getMeasuredWidth() / 2, stickView.getMeasuredHeight
                        () + 1);
                if (transInfoView != null && transInfoView.getTag() != null) {
                    int transViewStatus = (int) transInfoView.getTag();
                    int dealtY = transInfoView.getTop() - stickView.getMeasuredHeight();
                    if (transViewStatus == FoodAdapter.HAS_STICKY_VIEW) {
                        if (transInfoView.getTop() > 0) {
                            stickView.setTranslationY(dealtY);
                        } else {
                            stickView.setTranslationY(0);
                        }
                    } else if (transViewStatus == FoodAdapter.NONE_STICKY_VIEW) {
                        stickView.setTranslationY(0);
                    }
                }
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        EventbusHelper.register(this);
    }

    @Override
    public void onStop() {
        EventbusHelper.unregister(this);
        super.onStop();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMsgEvent(NetMsgEvent event) {
        String _msgType = (String) event.getMsgType();
        JSONObject jsonObject = event.getMsgJSONObj();
        switch (_msgType) {
            case "MINE_COLLECT":
                setmRefreshLayout();
                if (jsonObject != null) {
                    loadingUtil.stopShowLoading();
                    loadingDate(jsonObject);
                }
                break;
        }
    }
    private void setmRefreshLayout() {
        mRefreshLayout.finishRefresh();
        mRefreshLayout.finishLoadmore();
    }
    private void loadingDate(JSONObject jsonObject) {
        if (jsonObject.optString("error").equals("0")) {
            JSONObject object1 = jsonObject.optJSONObject("data");
            JSONArray jsonArray = object1.optJSONArray("items");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject object = jsonArray.optJSONObject(i);
                if (i == 0) {
                    tvStickyHeaderView.setText(object.optString("contact"));
                }
                CollectOrderEntity collectOrderEntity = GsonResolve.jsonString2Bean(object.toString(), CollectOrderEntity.class);
                collectOrderEntity.setType(object.optString("contact"));
                collectOrderEntities.add(collectOrderEntity);
            }
            if (collectOrderEntities.size() == 0) {
                mEmptyLayout.setVisibility(View.VISIBLE);
            }else {
                mEmptyLayout.setVisibility(View.GONE);
            }
            mAdapter = new CollectOrderAdapter(collectOrderEntities);
            mAdapter.bindToRecyclerView(mRv);
            View view = new View(this);
            mAdapter.addFooterView(view);
            view.setMinimumHeight(ViewUtils.dip2px(this, 50));
            mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Intent intent = new Intent(CollectOrderActivity.this, OrderCarDatelisActivity.class);
                    intent.putExtra("shop_id", mAdapter.getItem(position).getShop_id());
                    startActivity(intent);
                }
            });
        } else {
            mEmptyLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        if (getIntent().getStringExtra("mine_collect").equals("mine_collect")) {
            ServiceUrl.LoadcollectSerVice(this, "MINE_COLLECT");
        }
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {

    }
}
